<?php

namespace app\admin\controller;
use think\Controller;
use think\File;
use think\Request;
use OSS\OssClient;
use OSS\Core\OssException;

class Upload extends Base
{
	//阿里云oss上传
    public function upload_oss() {
		//$file = request()->file('file');
		$request = Request::instance();
		$file = $request->file('file');
        if (!$file) return ['code' => 1, 'msg' => '未上传文件'];
        
        $config = config('aliyun_oss');
        try {
            $ossClient = new OssClient(
                $config['KeyId'],
                $config['KeySecret'],
                $config['Endpoint']
            );
            
            // 生成唯一文件名：目录/年月日/随机ID.后缀
            $extension = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
            $object =  date('Ymd') . '/' . uniqid() . '.' . $extension;
            // 执行上传（直接使用临时文件路径）
            $result = $ossClient->uploadFile(
                $config['Bucket'],
                $object,
                $file->getRealPath()
            );
            
			if(empty($result['info']['url'])){
				throw new \think\Exception('oss接口出错');
			}
            
            
        } catch (OssException $e) {
            //return ['code' => 1, 'msg' => $e->getMessage()];
			return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
		
		if(strpos($result['info']['url'],'http://') !== false)$result['info']['url'] = str_replace("http://", "https://", $result['info']['url']);
		// 返回OSS文件URL
		/* return [
			'code' => 0,
			'data'  => ['src'=>$result['info']['url']] // 公开访问URL
		]; */
		return json(['code' => 0,'data'  => ['src'=>$result['info']['url']]]);
    }

	
    public function upload_oss_video() {
		//$file = request()->file('file');
		$request = Request::instance();
		$file = $request->file('file');
        if (!$file) return ['code' => 1, 'msg' => '未上传文件'];
        
        $config = config('aliyun_oss');
        try {
            $ossClient = new OssClient(
                $config['KeyId'],
                $config['KeySecret'],
                $config['Endpoint']
            );
            
            // 生成唯一文件名：目录/年月日/随机ID.后缀
            $extension = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
            $object =  'videos/'.date('Ymd') . '/' . uniqid() . '.' . $extension;
            
            // 执行上传（直接使用临时文件路径）
            $result = $ossClient->uploadFile(
                $config['Bucket'],
                $object,
                $file->getRealPath()
            );
            
            if(strpos($result['info']['url'],'http://') !== false)$result['info']['url'] = str_replace("http://", "https://", $result['info']['url']);
            // 返回OSS文件URL
            return [
                'code' => 0,
                'data'  => ['src'=>$result['info']['url']] // 公开访问URL
            ];
        } catch (OssException $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
	//图片上传
    public function upload(){
       $file = request()->file('file');
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
       if($info){
            echo $info->getSaveName();
        }else{
            echo $file->getError();
        }
    }
	
    public function upload_xc(){
       $file = request()->file('file');
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
	   //dump($info->getSaveName());die;
       if($info){
            //echo '/uploads/images'.$info->getSaveName();
			$msg=['code'=>0,'msg'=>'上传成功','data'=>['src'=>'/uploads/images/'.$info->getSaveName()]];
        }else{
            //echo $file->getError();
			$msg=['code'=>1,'msg'=>$file->getError()];
        }
		//dump($msg);die;
		return $msg;
    }
	
	//图片上传返回url
    public function upload_url(){
       $file = request()->file('file');
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
       if($info){
			$msg=['code'=>0,'msg'=>'上传成功','data'=>['src'=>config('app_host').'/uploads/images/'.$info->getSaveName()]];
        }else{
			$msg=['code'=>1,'msg'=>$file->getError()];
        }
		return $msg;
    }

    public function upload_video(){
       $file = request()->file('file');
       $info = $file->validate(['size'=>1024*1024*500,'ext'=>'mp4'])->move(ROOT_PATH . 'public' . DS . 'uploads/videos');
	   //dump($info->getSaveName());die;
       if($info){
            //echo '/uploads/images'.$info->getSaveName();
			$msg=['code'=>0,'msg'=>'上传成功','data'=>['src'=>config('app_host').'/uploads/videos/'.$info->getSaveName()]];
        }else{
            //echo $file->getError();
			$msg=['code'=>1,'msg'=>$file->getError()];
        }
		//dump($msg);die;
		return $msg;
    }
	
    public function upload_pdf(){
       $file = request()->file('file');
       $info = $file->validate(['size'=>1024*1024*10,'ext'=>'pdf'])->move(ROOT_PATH . 'public' . DS . 'uploads/images');
	   //dump($info->getSaveName());die;
       if($info){
            //echo '/uploads/images'.$info->getSaveName();
			$msg=['code'=>0,'msg'=>'上传成功','data'=>['src'=>'/uploads/images/'.$info->getSaveName()]];
        }else{
            //echo $file->getError();
			$msg=['code'=>1,'msg'=>$file->getError()];
        }
		//dump($msg);die;
		echo json_encode($msg);
    }
    public function upload_pdf_url(){
       $file = request()->file('file');
       $info = $file->validate(['size'=>1024*1024*10,'ext'=>'pdf'])->move(ROOT_PATH . 'public' . DS . 'uploads/images');
	   //dump($info->getSaveName());die;
       if($info){
            //echo '/uploads/images'.$info->getSaveName();
			$msg=['code'=>0,'msg'=>'上传成功','data'=>['src'=>config('app_host').'/uploads/images/'.$info->getSaveName()]];
        }else{
            //echo $file->getError();
			$msg=['code'=>1,'msg'=>$file->getError()];
        }
		//dump($msg);die;
		echo json_encode($msg);
    }
	
    public function upload_excle(){
       $file = request()->file('file');
       $info = $file->validate(['size'=>1024*1024*10,'ext'=>'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads/images');
	   //dump($info->getSaveName());die;
       if($info){
            //echo '/uploads/images'.$info->getSaveName();
			$msg=['code'=>1,'msg'=>'上传成功','data'=>['src'=>'/uploads/images/'.$info->getSaveName()]];
        }else{
            //echo $file->getError();
			$msg=['code'=>0,'msg'=>$file->getError()];
        }
		//dump($msg);die;
		echo json_encode($msg);
    }
    //会员头像上传
    public function uploadface(){
       $file = request()->file('file');
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/face');
       if($info){
            echo $info->getSaveName();
        }else{
            echo $file->getError();
        }
    }

    public function getRegion()
    {
        $key = input('parent_id');  //商品名称
        $data = db('region')->where('parent_id',$key)->field('id,name')->select();
        $html = '';
        if($data)
        {
            foreach($data as $h){
                $html .= "<option value='{$h['id']}'>{$h['name']}</option>";
            }
        }
        return json(['code' => 200, 'data' => $html, 'msg' => '成功']);
    }

}